package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjq.allservice.entity.Goods;
import com.cjq.allservice.entity.Type;
import com.cjq.allservice.entity.vo.GoodsQuery;
import com.cjq.allservice.entity.vo.TypeQuery;
import com.cjq.allservice.service.GoodsService;
import com.cjq.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/allservice/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @ApiOperation("添加商品")
    @PostMapping("/addGoods")
    public R addGoods(@RequestBody Goods goods){
        boolean save = goodsService.save(goods);
        System.out.println(goods.getGoodsId());
        if (save==true){
            return R.succ().data("goodsId",goods.getGoodsId()).message("添加商品成功！");
        }else{
            return R.fail().message("添加商品失败！");
        }
    }

    @ApiOperation("获取所有商品")
    @GetMapping("/getAllGoods")
    public R getAllGoods(){
        List<Goods> list = goodsService.list();
        return R.succ().data("goodsList",list);
    }


    @ApiOperation("修改商品信息")
    @PostMapping("/updateGoods")
    public R updateGoods(@RequestBody Goods goods){
        boolean b = goodsService.updateById(goods);
        if (b==true){
            return R.succ().message("修改成功！");
        }else{
            return R.fail().message("修改失败！");
        }
    }

    @ApiOperation("根据id获取商品的信息")
    @GetMapping("/getGoodsById")
    public R getGoodsById(@RequestParam String id){
        Goods goods = goodsService.getById(id);
        return R.succ().data("goods",goods);
    }

    @ApiOperation("根据goodsId进行商品的删除")
    @DeleteMapping("/deletegoods")
    public R deletegoods(@RequestParam String goodsId){
        boolean b = goodsService.removeById(goodsId);
        if (b==true){
            return R.succ().message("删除成功！");
        }else {
            return R.fail().message("删除失败");
        }
    }

    @ApiOperation("条件查询商品带分页")
    //条件查询带分页
    @PostMapping ("/pageGoodsCondition/{current}/{limit}")
    public R pageGoodsCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) GoodsQuery goodsQuery){
        Page<Goods> page=new Page<>(current,limit);
        Page<Goods> page1=new Page<>(current,limit);
        Page<Goods> page2=new Page<>(current,limit);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        QueryWrapper<Goods> wrapper1 = new QueryWrapper<>();
        QueryWrapper<Goods> wrapper2 = new QueryWrapper<>();

        //多条件组合查询
        String id = goodsQuery.getGoodsId();
        String name = goodsQuery.getGoodsName();
        String content = goodsQuery.getGoodsContent();
        if (!StringUtils.isEmpty(id)){
            wrapper.like("goods_id",id);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("goods_text",name);
        }
        if (!StringUtils.isEmpty(content)){
            wrapper.eq("goods_info",content);
        }
        wrapper.orderByDesc("update_time");
        wrapper1.orderByDesc("goods_sell");
        wrapper2.orderByDesc("goods_third_price");


        goodsService.page(page,wrapper);
        goodsService.page(page1,wrapper1);
        goodsService.page(page2,wrapper2);

        long total=page.getTotal();
        System.out.println(total);
        List<Goods> records = page.getRecords();
        List<Goods> records1 = page1.getRecords();
        List<Goods> records2 = page2.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        map.put("rowsOne",records1);
        map.put("rowsTwo",records2);

        return R.succ().data(map);
    }


    @ApiOperation("条件查询商品带分页小程序")
    //条件查询带分页
    @PostMapping ("/pageGoodsConditionPhone/{current}/{limit}/{typeId}")
    public R pageGoodsConditionPhone(@PathVariable long current, @PathVariable long limit, @PathVariable String typeId,@RequestBody(required = false) GoodsQuery goodsQuery){
        Page<Goods> page=new Page<>(current,limit);
        Page<Goods> page1=new Page<>(current,limit);
        Page<Goods> page2=new Page<>(current,limit);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        QueryWrapper<Goods> wrapper1 = new QueryWrapper<>();
        QueryWrapper<Goods> wrapper2 = new QueryWrapper<>();

        //多条件组合查询
        String id = goodsQuery.getGoodsId();
        String name = goodsQuery.getGoodsName();
        String content = goodsQuery.getGoodsContent();
        if (!StringUtils.isEmpty(id)){
            wrapper.like("goods_id",id);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("goods_text",name);
        }
        if (!StringUtils.isEmpty(content)){
            wrapper.eq("goods_info",content);
        }
        wrapper.eq("goods_type_id",typeId);
        wrapper1.eq("goods_type_id",typeId);
        wrapper2.eq("goods_type_id",typeId);
        wrapper.orderByDesc("update_time");
        wrapper1.orderByDesc("goods_sell");
        wrapper2.orderByDesc("goods_third_price");


        goodsService.page(page,wrapper);
        goodsService.page(page1,wrapper1);
        goodsService.page(page2,wrapper2);

        long total=page.getTotal();
        System.out.println(total);
        List<Goods> records = page.getRecords();
        List<Goods> records1 = page1.getRecords();
        List<Goods> records2 = page2.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        map.put("rowsOne",records1);
        map.put("rowsTwo",records2);

        return R.succ().data(map);
    }



}

